<?php

class VsOrderAddress {
	protected $_sqlTable = 'order_addresses';

	protected $_id = 0;

	protected $_orderId = 0; // Deprecated
	protected $_order = NULL;

	protected $_country = '';
	protected $_province = '';
	protected $_city = '';
	protected $_postCode = '';
	protected $_address = '';

	protected $_title = '';
	protected $_firstName = '';
	protected $_lastName = '';

	protected $_phone = '';
	protected $_mail = '';

	protected $_type = 'billing';

	private $_saved = FALSE;

	static function makeNewBilling(&$order) {
		
		$addr = new VsOrderAddress(&$order);

		$addr->_id = 0;
		$addr->_orderId = $order->id();

	/*
		$addr->_country = '';
		//$addr->_province = '';
		$addr->_city = '';
		$addr->_postCode = '';
		$addr->_address = '';

		$addr->_title = '';
		$addr->_firstName = '';
		$addr->_lastName = '';
		$addr->_phone = '';
		$addr->_mail = '';
	*/

		return $addr;
	}
	//================================================================

	static function makeFromUser(&$order, $formArray) {
		
		$addr = new VsOrderAddress(&$order);

		//$addr->_id = $formArray['id'];
		$addr->_orderId = $formArray['order_id'];
		$addr->_country = $formArray['country'];
		//$addr->_province = $formArray['stateProv'];
		$addr->_city = $formArray['city'];
		$addr->_postCode = $formArray['zipCode'];
		$addr->_address = $formArray['address1'];

		$addr->_title = isset($formArray['title']) ? $formArray['title'] : 'Not Defined';
		$addr->_firstName = $formArray['firstName'];
		$addr->_lastName = $formArray['lastName'];
		$addr->_phone = $formArray['phone1'];
		$addr->_mail = $formArray['email'];

		$addr->_type = isset($formArray['type']) ? $formArray['type'] : "billing";

		return $addr;
	}
	//================================================================

	static function makeFromDb(&$order, $id) {
		//echo $id;
		//exit;

		if(!$id)
			return NULL;

		$db = VsDatabase::Instance();
		$q = "SELECT * FROM `order_addresses` WHERE id=".$id;
		$res = $db->query($q);
		$resAddr = mysql_fetch_object($res);

		if(!$resAddr)
			return NULL;

		$addr = new VsOrderAddress(&$order);

		$addr->_id = $resAddr->id;
		$addr->_orderId = $resAddr->orders_id;
		$addr->_country = $resAddr->country;
		$addr->_province = $resAddr->province;
		$addr->_city = $resAddr->city;
		$addr->_postCode = $resAddr->postal_code;
		$addr->_address = stripslashes($resAddr->address);

		$addr->_title = $resAddr->title;
		$addr->_firstName = $resAddr->first_name;
		$addr->_lastName = $resAddr->last_name;

		$addr->_phone = $resAddr->telephone;
		$addr->_mail = $resAddr->mail;

		if($resAddr->type)
			$addr->_type = $resAddr->type;


		return $addr;
	}
	//=================================================================

	protected function __construct(&$order) {
		$this->_order = &$order;

		//echo $this->_order->id();
		//exit;
	}
	//=================================================================

	function id() { return $this->_id; }
	function orderId() { return $this->_order->id(); }
	function country() { return $this->_country; }
	function province() { return $this->_province; }
	function city() { return $this->_city; }
	function postalCode() { return $this->_postCode; }
	function postCode() { return $this->_postCode; }
	function address() { return stripslashes($this->_address); }
	function firstName() { return $this->_firstName; }
	function lastName() { return $this->_lastName; }
	function phone() { return $this->_phone; }
	function mail() { return $this->_mail; }

	//=================================================================

	function delete() {
		if(!$this->_id)
			return;

		$q = "DELETE FROM `".$this->_sqlTable."` WHERE id=".$this->_id;

		$this->_db->query($q);
	}
	//=================================================================

	function save() {
		if($this->_order->id() < 1) {
			//echo "Order Id: ".$this->_order->id();
			//exit;

			return;
		}

		//echo "Order Address Id: ".$this->_id;
		//exit;

		if($this->_id > 0)
			$this->update();
		else
			$this->insert();
	}
	//=================================================================

	protected function insert() {
		$db = VsDatabase::Instance();


		$q = "INSERT INTO `".$this->_sqlTable."` (`orders_id`, `title`, `first_name`, `last_name`, `mail`, `country`, `province`, `city`, `postal_code`, `address`, `telephone`, `type`) VALUES('".$this->_order->id()."', '".$this->_title."', '".$this->_firstName."', '".$this->_lastName."', '".$this->_mail."', '".$this->_country."', '".$this->_province."', '".$this->_city."', '".$this->_postCode."', '".addslashes($this->_address)."', '".$this->_phone."', '".$this->_type."')";
		//echo $q;

		$db->query($q);
		//echo mysql_error();

		$this->_id = mysql_insert_id();
	}
	//=================================================================

	protected function update() {
		$db = VsDatabase::Instance();

		/*
		$q = "UPDATE `".$this->_sqlTable."` SET `products_categories_id`='".$this->_categoryId."', `name`='".addslashes($this->_name)."', `info`='".addslashes($this->_info)."', date=NOW() WHERE id=".$this->_id;

		$db->query($q);

		*/
	}
	//==================================================================

};

?>
